/*
 * @Author: pengfei
 */

 import DealRequest from '../interface/dealRequest.interface';
 import unicore from '../core.service';
 interface JCQQSJ {
    /** 应用版本号 */
    yybbh? : string,
    /** 渠道编号 */
    qdbh : string,
    /** 渠道类型 */
    qdlx : string,
    /** 终端编号 */
    zdbh? : string,
    /** 制造型号标识 */
    zzxhbs? : string,
    /** 交易代码  */
    jydm?: string,
 }

 interface YWQQJS {
    /** 加密数据 */
    jmsj : string,
    /** 数据摘要 */
    sjzy : string,
    /** 数字信封 */
    szxf : string,
 }

 export default class XcjhHeader implements DealRequest {
    jcqqsj :JCQQSJ = <JCQQSJ>{};
    ywqqsj :YWQQJS = <YWQQJS>{};
    encrapt(data:any){
        // md5 加密
        const timestamp = new Date().valueOf();
        const datastr = JSON.stringify(data)
        const md5str = unicore.util.crypto.MD5encrypt(datastr,timestamp);
        this.ywqqsj.sjzy = md5str;//数据摘要(MD5)：报文体|时间戳|16位随机秘钥
        // AES 加密报文
        const aesstr = unicore.util.crypto.encrypt(datastr);
        this.ywqqsj.jmsj = aesstr;//加密数据(AES)：报文体
        // 签名
        const szxf = unicore.util.crypto.EncryptKey(timestamp);
        this.ywqqsj.szxf = szxf;//数字信封(RSA)：16位随机秘钥|时间戳
        return {
            jcqqsj:this.jcqqsj,
            ywqqsj:this.ywqqsj
        }

    }
    /**解密函数，当fhm!==000000时不解析 */
    decrapt(res:any,noshowToast?:boolean){
        if (res.jcxysj.fhm==-9992) {
            /**登录超时特殊返回码 */
            const route = unicore.dom.window.location.hash.replace(/\#/g,"")
            let local_url = '/pages/login/indexty?redirect='+ route
            uni.redirectTo({url:local_url})
            return;
        }else if(res.jcxysj.fhm != "000000" && res.jcxysj.fhm != "091203"&& res.jcxysj.fhm != "021005"){
            /**000000-成功返回码，091203-兴才特殊返回码 */
            return;
        }else if(res.jcxysj.fhm == "091203"){
            //兴才特殊判断，24小时外签订返回报文
            /**000000-成功返回码，091203-兴才特殊返回码 */
            let jmsj = {
                fhm: '091203',
                fhmsxx: '提交成功！请您通知您的家人在24小时内进行签订'
            }
            return jmsj;
        }else if(res.jcxysj.fhm == "021005"){
            // 未实名返回报文
            let jmsj = {
                fhm: '021005',
                fhmsxx: '请您先进行实名认证后，再为您办理查询贷款余额业务哦！'
            }
            return jmsj;
        }else{
            /**返回码为000000时解析逻辑 */
            let rsa = unicore.util.crypto.decrptKey(res.ywxysj.szxf)
            let keytime = rsa.split('|')
            let jmsj = unicore.util.crypto.decrypt(res.ywxysj.jmsj,keytime[0])
            let mddata = unicore.util.crypto.MD5encrypt(jmsj,keytime[1],keytime[0]).toUpperCase(); // MD5
            if (mddata==res.ywxysj.sjzy) {
                return jmsj;
            }else{
                uni.showToast({
                    title:'参数解析异常',
                    icon:'none',
                    duration:2000
                })
            }
        }
    }
    getHeader(header?:any){
        return Object.assign({},header,{
            'Content-Type': 'application/json;charset=UTF-8;',
            'X_ClientToken': unicore.config.X_ClientToken,
            'X_GwApiToken': unicore.config.X_GwApiToken
        })
    }
    constructor(qdbh:string,qdlx:string,yybbh?:string){
        this.jcqqsj.qdbh = qdbh;
        this.jcqqsj.qdlx = qdlx;
        this.jcqqsj.yybbh = yybbh;
    }
 }